/**
 * 辅助routeHack 里面的第二点 《识别IOS浏览器左划手势的后退功能》 方案
 * 1、用来做切换页面的过渡动画， 虽然可以写常规的css3动画来达到效果， 
 *   但是如果使用了css3动画，在iPhone常规浏览器上使用左划返回时（微信左划直接关闭整个浏览器窗口返回微信应用中去了）动画会再执行一遍
 * 
 * @author LinRenChen
 *
 * Created at     : 2018-09-09 02:25:35 
 * Last modified  : 2019-05-14 22:20:10
 */

import Vuex from 'vuex';
import Vue from 'vue/dist/vue.js'
import fetch from './fetch/index';
Vue.use(Vuex);

const shouldUseTransition = !/transition=none/.test(location.href);

let store = new Vuex.Store({
    state: {
        navHeadrName: '',

        userInfo: {
            authentStatus: '',
            checkUserId: "",
            clientType: '',
            createTime: "",
            isLoginDisable: '',
            isPayDisable: '',
            loginLockStatus: '',
            loginPwdErrNum: '',
            mobile: "",
            mobileArea: "",
            originTag: "",
            payLockStatus: '',
            payPwdErrNum: '',
            sex: '',
            token: "",
            userId: '',
            userName: "",
            userStatus: '',
            weixin: "",
            openId: ''
        },
    },


    mutations: {
        updateUserInfo(state, data) {
            for(let i in data) {
                state.userInfo[i] = data[i];
            }
        },


        clearUserInfo(state) {
            let userInfo = {
                authentStatus: '',
                checkUserId: "",
                clientType: '',
                createTime: "",
                isLoginDisable: '',
                isPayDisable: '',
                loginLockStatus: '',
                loginPwdErrNum: '',
                mobile: "",
                mobileArea: "",
                originTag: "",
                payLockStatus: '',
                payPwdErrNum: '',
                sex: '',
                token: "",
                userId: '',
                userName: "",
                userStatus: '',
                weixin: "",
                openId: '',
            };

            state.userInfo = userInfo;
            localStorage.removeItem('token');
        }
    },
    actions: {
        getUserInfo({commit}, token) {
            return new Promise((reslove, reject) => {
                fetch.post('/user/auth/front/userTokenLogin', {
                    token
                }).then(res => {
                    if (res.status === 0) {
                        commit('updateUserInfo', res.data);
                        reslove(res.data);
                    }
                })
            })
        }
    }
});
  


store.registerModule('vux', {
    state: {
        demoScrollTop: 0,
        isLoading: false,
        direction: shouldUseTransition ? 'forward' : '',
        
        contentHeight: '',
        bar: true,
        headTitle: '',
    },


    getters: {

    },
    mutations: {
        updateDemoPosition(state, payload) {
            state.demoScrollTop = payload.top;
        },
        updateLoadingStatus(state, payload) {
            state.isLoading = payload.isLoading;
        },
        updateDirection(state, payload) {
            if (!shouldUseTransition) {
                return;
            }
            state.direction = payload.direction;
        },

    },

    actions: {
        updateDemoPosition({
            commit
        }, top) {
            commit({
                type: 'updateDemoPosition',
                top: top
            });
        },
    },
});


export default store;
